import 'package:flutter/material.dart';
import 'package:xws/helpers/safe_state.dart';
import 'package:xws/models/goods/goods_obj.dart';
import 'package:xws/res/icons.dart';
import 'package:xws/utils/dio_utils/request/goods_req.dart';
import 'package:xws/utils/theme_util.dart';
import 'package:xws/utils/tools_util.dart';
import 'package:xws/widgets/app_btn.dart';
import 'package:xws/widgets/app_text_field.dart';
import 'package:xws/widgets/goods_config/goods_item.dart';
import 'package:xws/widgets/refresh_widget/refresh_load_view.dart';
import 'package:xws/widgets/safe_appbar.dart';
import 'package:xws/widgets/safe_scaffold.dart';

/// 搜索物品
class SearchGoods extends StatefulWidget {
  const SearchGoods({Key? key, this.refresh}) : super(key: key);

  @override
  _SearchGoodsState createState() => _SearchGoodsState();

  final Future<void> Function()? refresh;
}

class _SearchGoodsState extends State<SearchGoods> with SafeState<SearchGoods> {
  late RefreshViewController<GoodsObj> _viewController;
  late FocusNode _focusNode;

  String _key = '';

  bool get _isKeyEmpty => _key.trim().isEmpty;

  @override
  void initState() {
    super.initState();
    _viewController = RefreshViewController<GoodsObj>();
    _focusNode = FocusNode();
    delayed(const Duration(milliseconds: 400)).then((_) => _focusNode.requestFocus());
  }

  @override
  void dispose() {
    _viewController.dispose();
    _focusNode.dispose();
    super.dispose();
  }

  /// 搜索框值变化
  void _search() {
    if (_isKeyEmpty) return;

    unFocus(context);

    _viewController.refresh();
  }

  @override
  Widget build(BuildContext context) {
    return SafeScaffold(
      resizeToAvoidBottomInset: false,
      appBar: SafeAppBar(
        title: Padding(
          padding: const EdgeInsets.symmetric(vertical: 2),
          child: AppTextField(
            focusNode: _focusNode,
            hintText: '搜索附近好物...',
            onChanged: (String v) => _key = v,
            maxLength: 16,
            border: OutlineInputBorder(
              borderSide: BorderSide.none,
              borderRadius: BorderRadius.circular(6),
            ),
            fillColor: canvas(context),
            contentPadding: const EdgeInsets.symmetric(horizontal: 20, vertical: 12),
            textInputAction: TextInputAction.search,
            onEditingComplete: _search,
          ),
        ),
        actions: <Widget>[
          AppBtn.noPadding(
            onTap: _search,
            width: 38,
            height: 38,
            color: Colors.transparent,
            child: const Icon(IR.search, size: 20),
          ),
        ],
      ),
      body: _buildList,
    );
  }

  Widget get _buildList {
    return RefreshLoadView<GoodsObj>(
      type: ViewType.waterfall,
      viewController: _viewController,
      padding: const EdgeInsets.all(20),
      onRefresh: (int v, _) async {
        if (_isKeyEmpty) return <GoodsObj>[];
        return await getGoodsList(keyword: _key, pageNum: 1);
      },
      onLoad: (int page, _) async {
        if (_isKeyEmpty) return <GoodsObj>[];
        return await getGoodsList(keyword: _key, pageNum: page);
      },
      itemBuilder: (int index, GoodsObj g) => GoodsItem(index: index, goodsObj: g),
    );
  }
}
